<template>
    <div class='message-center-container'>
        <div class='header'>
            <a-tabs v-model='activeName' @change="callback">
                <a-tab-pane key="1" tab="系统通知"></a-tab-pane>
                <!--                <a-tab-pane key="2" tab="交易通知"></a-tab-pane>
                <a-tab-pane key="3" tab="服务消息"></a-tab-pane>-->
            </a-tabs>
            <div class='opera-box'>
                <div class='left'>
                    <p>有<span>{{ count }}</span>条系统通知未读</p>
                    <div class='img-btn' @click='toAllRead'><img src='~/assets/images/message-have-read.png' alt=''><span>全部设为已读</span></div>
                </div>
                <div class='btn-box'>
                    <a-button class="green-border" v-if='!isDelete' @click='isDelete = true'>批量删除</a-button>
                    <a-button class="green-border delete" v-if='isDelete' @click='toDelete()'>删除选中</a-button>
                    <a-button class="green" v-if='isDelete' @click='closeDelete'>取消</a-button>
                    <a-button class="green" type='primary' @click='$router.go(-1)'>返回</a-button>
                </div>
            </div>
        </div>
        <div class='check-all' :class="{'is-delete': isDelete}">
            <a-checkbox v-if='isDelete' :indeterminate="indeterminate" :checked="checkAll" @change="onCheckAllChange">全选</a-checkbox>
        </div>
        <div class='content'>
            <a-checkbox-group :value='checkList' @change="onChange">
                <div class='list'>
                    <div class='item' v-for='(item, index) in listData' :key='index'>
                        <div class='left'>
                            <div class='label' :class="{'is-delete': isDelete}">
                                <a-checkbox :value="item.id" v-if='isDelete'></a-checkbox>
                            </div>
                            <div class='left-inner'>
                                <p class='title'>
                                    <span class='not-read' v-if='!item.readFlag'>未读</span>
                                    <span class='name'>{{ item.title }}</span>
                                    <span class='time'>{{ item.createTime }}</span>
                                </p>
                                <div class='word'>
                                    {{ item.message }}
                                    <template v-if='[3, 5].includes(item.category)'>
                                        ,立即<span @click='toHandle(1)'>前往查看原因</span>
                                    </template>
                                    <template v-else-if='[2, 4].includes(item.category)'>
                                        ,立即<span @click='toHandle(2)'>前往开启店铺经营</span>
                                    </template>
                                    <template v-else-if='[6].includes(item.category)'>
                                        ,请<span @click='toHandle(3)'>前往审核</span>
                                    </template>
                                </div>
                            </div>
                        </div>
                        <div class='right'>
                            <div class='img-btn delete' @click='toDelete(item.id)'><img src='~/assets/images/message-delete.png' alt=''><span>删除</span></div>
                            <div class='img-btn' v-if='!item.readFlag' @click='toRead(item.id)'><img src='~/assets/images/message-have-read.png' alt=''><span>标记为已读</span></div>
                            <div class='img-btn' v-else><img src='~/assets/images/message-have-read.png' alt=''><span>已读</span></div>
                        </div>
                    </div>
                </div>
            </a-checkbox-group>
            <custom-pagination :total="Number(total)" :page.sync="pageIndex" :limit.sync="pageSize" @pagination="getTable"></custom-pagination>
        </div>
    </div>
</template>

<script>
import { mapActions } from 'vuex'
import { localStorage } from '~/plugins/tools'
export default {
    name: 'MessageCenterN',
    layout: 'no-menu',
    data () {
        return {
            activeName: '1',
            listData: [],
            total: 0,
            pageIndex: 1,
            pageSize: 10,
            indeterminate: false,
            checkAll: false,
            checkList: [],
            isDelete: false,
            count: 0
        }
    },
    computed: {
        allId () {
            return this.listData.map(item => item.id)
        }
    },
    mounted () {
        this.getTable()
        this.SetCount(99)
    },
    methods: {
        ...mapActions('global', ['SetCount']),
        callback () {
            this.pageIndex = 1
            this.getTable()
        },
        async getTable () {
            const res = await this.$ApiServer['message/list']({
                currentPage: this.pageIndex,
                pageSize: this.pageSize,
                userId: localStorage.getItem('userInfo').id,
                // orgCategoryId: localStorage.getItem('userInfo').orgCategoryId,
                type: this.activeName
            })
            if (res.code === 200) {
                this.listData = res.data.data.page.records || []
                this.total = res.data.data.page.total
                this.count = res.data.data.count
                this.checkAll = false
                this.indeterminate = false
                this.checkList = []
                this.SetCount(this.count)
            } else {
                this.$message.error(res.message)
            }
        },
        toDelete (id) {
            if (!id && !this.checkList.length) return false
            this.$confirm({
                title: '提示',
                content: '确认是否删除选中消息',
                okText: '确认',
                cancelText: '取消',
                onOk: async () => {
                    const res = await this.$ApiServer['message/delete'](id ? [id] : this.checkList)
                    if (res.code === 200) {
                        this.closeDelete()
                        this.getTable()
                        this.$message.success(res.message)
                    } else {
                        this.$message.error(res.message)
                    }
                },
                onCancel () {}
            })
        },
        async toRead (id) {
            const res = await this.$ApiServer['message/read']([id])
            if (res.code === 200) {
                this.getTable()
                this.$message.success(res.message)
            } else {
                this.$message.error(res.message)
            }
        },
        async toAllRead () {
            const res = await this.$ApiServer['message/allRead']({
                type: this.activeName
            })
            if (res.code === 200) {
                this.getTable()
                this.$message.success(res.message)
            } else {
                this.$message.error(res.message)
            }
        },
        closeDelete () {
            this.isDelete = false
            this.checkAll = false
            this.indeterminate = false
            this.checkList = []
        },
        onChange (checkedList) {
            this.checkList = checkedList
            if (!this.checkList.length) {
                this.checkAll = false
                this.indeterminate = false
            } else if (this.checkList.length === this.allId.length) {
                this.checkAll = true
                this.indeterminate = false
            } else {
                this.checkAll = false
                this.indeterminate = true
            }
        },
        onCheckAllChange () {
            if (this.checkAll) {
                this.checkAll = false
                this.indeterminate = false
                this.checkList = []
            } else {
                this.checkAll = true
                this.indeterminate = false
                this.checkList = this.allId
            }
        },
        toHandle (type) {
            switch (type) {
            case 1:
                this.$router.push('/company-auth')
                break
            case 2:
                this.$router.push('/company-auth')
                break
            case 3:
                this.$router.push('/company-examine')
                break
            }
        }
    }
}
</script>

<style scoped lang='less'>
.img-btn {
    display: flex;
    align-items: center;
    margin-left: 30px;
    img {
        display: inline-block;
        width: 17px;
        height: 15px;
        margin-right: 2px;
    }
    font-size: 16px;
    line-height: 20px;
    color: #666;
    font-weight: 400;
    cursor: pointer;
    &.delete {
        color: #FD5454!important;
    }
}
.message-center-container {
    .header {
        background: #fff;
        border-radius: 6px;
        overflow: hidden;
        position: relative;
        .opera-box {
            display: flex;
            justify-content: space-between;
            align-items: center;
            height: 60px;
            padding-left: 20px;
            padding-right: 20px;
            .left {
                display: flex;
                align-items: center;
                .btn {
                    .img-btn {
                        &:extend(.img-btn);
                    }
                }
                p {
                    font-size: 16px;
                    line-height: 20px;
                    color: #2A2A2A;
                    font-weight: 400;
                    span {
                        color: #FD5454;
                    }
                }
            }
            button {
                height: 30px;
                line-height: 30px;
                font-size: 14px;
                font-weight: 400;
                color: #666666;
                &:hover {
                    color: #d15e5a;
                }
            }
            .btn-box {
              .green-border{
                color: @primary-color;
                border-color: @primary-color;
                background-color: #fff;
              }
              .green{
                color: #fff;
                border-color: @primary-color;
                background-color: @primary-color;
              }
            }
        }
        .goback {
            position: absolute;
            right: 16px;
            top: 6px;
        }
        ::v-deep {
            .ant-tabs {
                background: #fff;
            }
            .ant-tabs-bar{
              padding-left: 25px;
            }
            .ant-tabs-tab{
              color: #2A2A2A;
            }
            .ant-tabs-nav .ant-tabs-tab-active{
                color: #2A2A2A;
            }
            .ant-tabs-bar {
                margin-bottom: 0;
            }
        }
    }
    .check-all {
        padding: 5px 20px;
        font-size: 14px;
        font-weight: bold;
        line-height: 20px;
        color: #333333;
        height: 0;
        overflow: hidden;
        transition: height 0.5s;
        ::v-deep {
            .ant-checkbox-wrapper {
                margin-top: 5px;
            }
        }
        &.is-delete {
            height: 40px;
        }
    }
    .content {
        ::v-deep {
            .ant-checkbox-group {
                width: 100%;
            }
        }
        .list {
            .item {
                display: flex;
                justify-content: space-between;
                align-items: center;
                padding: 25px 20px;
                border-radius: 6px;
                background: #fff;
                margin-bottom: 6px;
                .left {
                    display: flex;
                    justify-content: flex-start;
                    align-items: flex-start;
                    width: calc(100% - 256px);
                    .label {
                        width: 0;
                        overflow: hidden;
                        padding-top: 17px;
                        transition: width 0.5s;
                        &.is-delete {
                            width: 36px;
                        }
                    }
                    .left-inner {
                        .title {
                            .not-read {
                                display: inline-block;
                                width: 40px;
                                height: 20px;
                                background: #FD5454;
                                border-radius: 4px;
                                text-align: center;
                                line-height: 20px;
                                font-size: 12px;
                                font-weight: 400;
                                color: #FFFFFF;
                                margin-right: 10px;
                            }
                            .name {
                                font-size: 14px;
                                font-weight: 400;
                                line-height: 20px;
                                color: #666;
                                margin-right: 40px;
                                margin-bottom: 10px;
                            }
                            .time {
                                font-size: 14px;
                                font-weight: 400;
                                line-height: 20px;
                                color: #666;
                            }
                        }
                        .word {
                            word-break: break-all;
                            font-size: 14px;
                            font-weight: 400;
                            line-height: 20px;
                            color: #2A2A2A;
                            margin-top: 10px;
                            span {
                                cursor: pointer;
                                color: @primary-color;
                                &:hover {
                                    border-bottom: 1px solid @primary-color;
                                }
                            }
                        }
                    }
                }
                .right {
                    display: flex;
                    align-items: center;
                    justify-content: flex-end;
                    width: 256px;
                    ::v-deep {
                        .ant-btn-link {
                            padding: 0 10px;
                        }
                    }
                    .delete {
                        display: none;
                    }
                    .img-btn {
                        &:extend(.img-btn);
                    }
                }
                &:hover {
                    .delete {
                        display: inline-block;
                    }
                }
            }
        }
    }
}
</style>
<style lang='less'>
.no-menu {
    .message-center-container {
        max-width: 1200px;
        margin: 10px auto 10px;
    }
}
</style>
